function [Traj,TrajInFrames] = makeTrajectoriesFromBinnedObjects(ObjectsContainedInFrames,velocityfield,params)
%Pixel radius of neighborhood size
%40X values in pixels
%global Traj
% ON=params.ON;
% TN=params.TN;
% CP=params.CP;
% binSize=params.binSize;
% NumberOfFramesToConsider=params.numOfPreviousFramesToConsider;
% 
% scoreCutoff=1;

%ObjectsContainedInFrame: cell array with each column containing a struct
%array of objects for a given frame.
%ObjectsContainedInFrame{}()

num_of_frames=length(ObjectsContainedInFrames);

%All initial objects will be active trajectories
[numOfObjectsInFrameOne,ONprops]=size(ObjectsContainedInFrames{1});

Traj=cell(numOfObjectsInFrameOne,1);

%make a cell array for all the trajectories in each frame.  Note this cell
%array should have a length equal to the number of frames, which is equal
%to the ObjectsContainedInFrames cell array
TrajInFrames=cell(length(ObjectsContainedInFrames),1);

%activeTraj=1:length(Traj);
TrajInFrames{1}=1:length(Traj);

%Initialize the TrajBin variable, a cell array with the current objects in
%the active trajectories binned
%Traj{trajectory number}(frame number)->struct containing the object
%information (note an object is uniquely specified by its frame and object
%numbers
for i=1:length(Traj)
    Traj{i}=zeros(1,length(fieldnames(params.TN)));
    Traj{i}(1,params.TN.frame)=1;
    Traj{i}(1,params.TN.object)=i;
    Traj{i}(1,params.TN.score)=NaN;
end

[Traj,TrajBin]=CreateTrajBinFromActiveTraj(1,TrajInFrames{1},ObjectsContainedInFrames,Traj,velocityfield,params);

%perform computation for each frame
%j is the frame index
%k is the trajectory index
for j=2:num_of_frames
%for j=2:50
    ['Trajectory loop outer: ' num2str(j) ' ' datestr(now)]
    
    
    %Intialize objects in current frame to contain the trajectory
    %processing data fields, especially important for parfor loops
    
   
    
    [Traj,TrajInFrames{j},ObjCalcProps]=ProcessSingleFrameForTrajectories(j,Traj,TrajBin,TrajInFrames{j-1},ObjectsContainedInFrames{j},params);
    %[Traj,activeTraj,ObjCalcProps]=ProcessSingleFrameForTrajectories(curFrame,Traj,TrajBin,activeTraj,ObjectsInCurrentFrame,params)
    [Traj,TrajBin]=CreateTrajBinFromActiveTraj(j,TrajInFrames{j},ObjectsContainedInFrames,Traj,velocityfield,params);
end
%TrajOut=Traj;
%clear global Traj

%     
%     ObjectsInCurrentFrame=ObjectsContainedInFrames{j};
%     [numOfObjectsInFrame,ONprops]=size(ObjectsInCurrentFrame);
%     ObjCalcProps=cell(numOfObjectsInFrame,1);
%     
%     %Process each object in the current frame and find trajectories in the
%     %objects neighborhood located in the TrajBin.  Score each object
%     %against all located trajectories and store
%     
%     parfor k=1:numOfObjectsInFrame      
%         ObjCalcProps{k}=CalculateObjectProperties(ObjectsInCurrentFrame(k,:),TrajBin,params);
%     end
% 
%     %At this point the objects should have scored all the trajectories in their neighborhoods.  Of course objects in this frame
%     %Will have been scored by multiple trajectories, so now we need to 
%     %go through all the objects and find the best match trajectory.  We
%     %will have to control for the case where two objects have the same
%     %trajectory as the best match.  In this case the best scoring object
%     %will be assigned to the trajectory and the poorer matching object will
%     %be kicked out and will go back to searching.  For objects with no best
%     %matching object new trajectories will started.
% 
%  
%     ObjectsToMatch=ObjectsInCurrentFrame(:,ON.Number);
%     %keep computing until all Objects have been either assigned to a
%     %current trajectory or a new trjectory is made for them
%     while ~isempty(ObjectsToMatch)
%         currentObject=ObjectsToMatch(end);
% 
%         %check to make sure at least one trajectory score was made if
%         %not add to a new trajectory.  Also make sure there are some active trajectories to
%         %compare, if not start a new trajectory
%         if(any(ObjCalcProps{currentObject}(:,CP.active)))
%            
%             active=ObjCalcProps{currentObject}(:,CP.active);
%             score=ObjCalcProps{currentObject}(:,CP.score);
%             BestMatchScore=min(score(logical(active)));
% 
%             %Make sure that the trajectory with the best score is
%             %above the score cutoff, if not start a new trajectory
%             if(BestMatchScore<scoreCutoff)
%                
%                 %Determine the best match index from the best match score.
%                 %Note there is a case when two trajectories have the same score
%                 %they will both come back as matching the best match index.
%                 %The second part of the comparison makes sure only the active
%                 %one is chosen. Also choose the first BestMatchIndex for
%                 %the case when there are two identical scores and both are
%                 %active
%                 BestMatchIndex=find(ObjCalcProps{currentObject}(:,CP.score)==BestMatchScore &...
%                     ObjCalcProps{currentObject}(:,CP.active)==1,1);
%                 
%                 BestMatchTraj=ObjCalcProps{currentObject}(BestMatchIndex,CP.trajNumber);
%                 %Determine if the best matching trajectory already
%                 %has a matching object assigned to it.  If not
%                 %assign this object and remove it from the search
% 
%                 [endIndex numOfTN]=size(Traj{BestMatchTraj});
%                 if(Traj{BestMatchTraj}(endIndex,TN.frame) ~= j)
%                     Traj{BestMatchTraj}(endIndex+1,TN.frame)=j;
%                     Traj{BestMatchTraj}(endIndex+1,TN.object)=ObjectsInCurrentFrame(currentObject,ON.Number);
%                     Traj{BestMatchTraj}(endIndex+1,TN.score)=ObjCalcProps{currentObject}(BestMatchIndex,CP.score);
%                                         
%                     ObjectsToMatch=RemoveObjectFromMatchGroup(ObjectsToMatch,ObjectsInCurrentFrame(currentObject,ON.Number));                 
%                 else
%                     %The best match trajectory already had a object
%                     %assigned, determine if the current object is a
%                     %better match.  If so swap objects, if not
%                     %remove this trajectory from the active list
%                     %and keep searching
%                     if(Traj{BestMatchTraj}(endIndex,TN.score)<ObjCalcProps{currentObject}(BestMatchIndex,CP.score))
%                         ObjCalcProps{currentObject}(BestMatchIndex,CP.active)=0; 
%                     else
%                         previousObjectNumber=Traj{BestMatchTraj}(endIndex,TN.object);
%                         
%                         Traj{BestMatchTraj}(endIndex,TN.object)=ObjectsInCurrentFrame(currentObject,ON.Number);
%                         Traj{BestMatchTraj}(endIndex,TN.score)=ObjCalcProps{currentObject}(BestMatchIndex,CP.score);
%                         ObjectsToMatch=RemoveObjectFromMatchGroup(ObjectsToMatch,ObjectsInCurrentFrame(currentObject,ON.Number));
% 
%                         [ObjectsToMatch,ObjCalcProps]=AddObjectToMatchGroup(ObjectsToMatch,ObjCalcProps,CP,previousObjectNumber,BestMatchTraj);                        
%                     end
%                     
%                 end
%                 %At this point the object has failed to generate a
%                 %best match trajectory, assign a new trajectory to
%                 %the object
%             else
%                 [Traj,activeTraj]=InsertNewTraj(Traj,TN,ObjectsInCurrentFrame(currentObject,ON.Number),activeTraj,j);     
%                 ObjectsToMatch=RemoveObjectFromMatchGroup(ObjectsToMatch,ObjectsInCurrentFrame(currentObject,ON.Number));
%             end           
%         else
%             [Traj,activeTraj]=InsertNewTraj(Traj,TN,ObjectsInCurrentFrame(currentObject,ON.Number),activeTraj,j);
%             ObjectsToMatch=RemoveObjectFromMatchGroup(ObjectsToMatch,ObjectsInCurrentFrame(currentObject,ON.Number));
%         end
%     end
%     %Find all trajectories that have not generated a hit in the specified
%     %time frame and effectively end the trajectory
%     [activeTraj]=PruneFromActiveTrajListBin(Traj,TN,j,activeTraj,NumberOfFramesToConsider);
%     [Traj,TrajBin]=CreateTrajBinFromActiveTraj(j,activeTraj,ObjectsContainedInFrames,Traj,velocityfield,params);
%     TrajInFrames{j}=activeTraj;
% end
        